/**
 * Created by Administrator on 2016/4/20.
 */
'use strict'
$(function() {
    var parallax1 = new Parallax(document.getElementById('bgcloud'), { limitY: 0, originX: 0 });
    var parallax2 = new Parallax(document.getElementById('bglgin'), { });
    $("#login1").click(postlogin);
   var lguser= getCookie("gislab_user");
    var lgpsw=getCookie("gislab_psw");
    if(lguser!=""&&lgpsw!=""){
        cookieLogin(lguser,lgpsw);
    }
});
function cookieLogin(user,psw){
    var params ={"username":user,"password":psw};
    $.ajax({
        url:"login",
        type:"POST",
        data:JSON.stringify(params),
        dataType:"json",
        contentType:"application/json",
        success:function(data){
            if(data!=undefined&&data!=null){
                var  isAutoLogin=document.getElementById("autologin_ckb").checked;
                var  _user=$("#user").val();
                var  _psw=$("#password").val();
                if(isAutoLogin && data!="/loginpage"){
                    setCookie("gislab_user",_user,7);
                    setCookie("gislab_psw",_psw,7);
                }
                window.location.href = "."+data;
            }
            else {
                layer.msg(langThis("login_error"));
            }


        },
        error:function(event){
            layer.msg(langThis("login_error"));
        }
    });
}
function  postlogin(){
    var usr=$("#user").val();
    var psw=$("#password").val();
    var params = {"username":usr,"password":psw};
    //使用jQuery中的$.ajax({});Ajax方法
    $.ajax({
        url:"login",
        type:"POST",
        data:JSON.stringify(params),
        dataType:"json",
    //   async: false,
        contentType:"application/json",
        success:function(data){
            /* 	if(data[0].user.user_role == "student") {
             window.location.href = "success_student";
             }else if(data[0].user.user_role == "admin") {
             window.location.href = "success_admin";
             }else if(data[0].user.user_role == "teacher") {
             window.location.href = "success_teacher";
             }  */
            if(data!=undefined&&data!=null){

                var  isAutoLogin=document.getElementById("autologin_ckb").checked;
                var  _user=$("#user").val();
                var  _psw=$("#password").val();
                if(isAutoLogin && data!="/loginpage"){
                    setCookie("gislab_user",_user,7);
                    setCookie("gislab_psw",_psw,7);
                }
                window.location.href ="."+data;
            }
            else {
                layer.msg(langThis("login_error"));
            }


        },
        error:function(event){
            layer.msg(langThis("login_error"));
        }
    });
}
function  keydown(){
    var x;
    if(window.event) // IE8 以及更早版本
    {
        x=event.keyCode;
    }
    else if(event.which) // IE9/Firefox/Chrome/Opera/Safari
    {
        x=event.which;
    }
    if(x==13){
        postlogin();
    }
}

